

//二叉搜索树
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    /**
     * 查找二叉搜索树中指定的val值
     *
     * @param val
     * @return
     */
    public TreeNode find(int val) {
        TreeNode cur = root;
//        if(null == cur) {
//            return null;
//        }
        while (cur != null) {
            if (cur.val > val) {
                cur = cur.right;
            } else if (cur.val < val) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入一个数据
     * @param val
     */
    public void insert(int val) {
        if(root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val){
                return;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        TreeNode node = new TreeNode(val);
        if(parent.val < val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
    }

    /**
     * 插入一个数据
     *
     * @param val
     */
    public void insert2(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }
            if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }
        }
        //走完之后判断前一个结点是否比val大
        if(cur.val < val) {
            //parent.right.val = val;
            parent.right = new TreeNode(val);
        }
        if(cur.val > val) {
            //parent.left.val = val;
            parent.left = new TreeNode(val);
        }
    }
    //显示打印
    public void inorder(TreeNode root) {
        if(null == root) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
        //System.out.print(root.val + " ");
    }



    /**
     * 删除值为val的节点
     * @param val
     */
    //自己练习写的
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null; //用于记录cur的前一个节点
        while(cur != null) {
            if(cur.val == val) {
                removeNode2(parent,cur);
                return;
            }else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            }
            if(target == targetParent.right) {
                targetParent.right = target.right;
            }
        }
    }

    //自己练习写的
    private void removeNode2(TreeNode parent, TreeNode cur) {
        //第一种情况 cur是root
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right; //此时cur所在节点是要删除的节点
                return;
            } else {
                if(cur == parent.right) {
                    //第二种情况 cur不是root，cur是parent.right
                    parent.right = cur.right;
                } else {
                    //第三种情况 cur不是root，cur是parent.left
                    parent.left = cur.right;
                }
            }
        } else if(cur.right == null) {
                if(cur == root) {     //第一种情况 cur是root
                    root = cur.left; //此时cur所在节点是要删除的节点
                    return;
                } else {
                    if(cur == parent.right) {
                        //第二种情况 cur不是root，cur是parent.right
                        parent.right = cur.left;
                    } else {
                        //第三种情况 cur不是root，cur是parent.left
                        parent.left = cur.left;
                    }
                }
        } else {     //cur左右都不为空节点  思路：找到cur右边节点最小的值与之进行交换就可以了
            TreeNode targetP = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                targetP = target;
                target = target.left;
            }
            //走到这说明target的左节点为空了
            //进行交换
            cur.val = target.val;
            if(target == targetP.left) {
                targetP.left = target.right; //target.right为空也没事，给到targetP.left，说明targetP节点左侧为空了
            }
            if(target == targetP.right) {
                targetP.right = target.right;  //这种情况是target没有左节点，相当于cur右边节点是个右单树
            }
        }
    }



    //这是之前做的一个题，用到之前学的非比较排序法
    public int firstUniqChar(String s) {
        int[] array = new int[s.length()];
        //遍历字符串，找出他们每个字符出现的次数
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            array[ch - 'a']++;
        }
        for (int i = 0; i < s.length(); i++) {
            if(array[i] == 1) {
                return  i;
            }
        }
        return -1;
    }
}
